//
//  UnarmedListVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/30.
//

import Foundation
import UIKit

class UnarmedListVC: SubLevelViewController {
    let header = UnarmedListHeaderView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 95))
    let tableView = UITableView(frame: .zero, style: .plain)
    
    var label: String?
    var val: String?
    
    var model: AlarmCbfdeptsModal?
    var datas: [AlarmCbfdeptsListpModal] = []
    
    var param: AlarmWBFParam = AlarmWBFParam()

    init(withDeptId deptId: Int64? = nil, deptName: String? = nil, date: String?, label: String?, val: String?) {
        super.init(withDeptId: deptId, deptName: deptName)
        self.label = label
        self.val = val
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        navigationController?.navBarStyle(.white)
        setupNavItems()
        self.title = "\(label ?? "今日未布防") - \(deptName ?? "")"
        param = AlarmWBFParam(date: date, deptId: "\(deptId ?? -1)", abfType: val)
        setupUI()
        reloadData()
    }
    
    func reloadData() {
        self.model = nil
        self.header.updateUI(withModal: self.model)
        self.datas = []
        view.showToastActivity()
        API.getAlarmCbfdepts(withParam: param) { responseModel in
            self.model = responseModel.model
            self.datas = responseModel.model?.list ?? []
            DispatchQueue.main.async {
                self.header.updateUI(withModal: self.model)
                self.tableView.reloadData()
                self.view.hideToastActivity()
            }
        }
    }
    
    @objc func searchTapped() {
        let searchVC = UnarmedSearchVC()
        searchVC.didSelectItemWith = { (item, dept) -> () in
            self.param.date = item.date
            self.param.deptId = item.deptId
            self.deptId = dept.deptId
            self.deptName = dept.deptName
            DispatchQueue.main.async {
                self.title = "\(self.label ?? "今日未布防") - \(dept.deptName ?? "")"
                self.reloadData()
            }
        }
        navigationController?.pushViewController(searchVC, animated: true)
    }
 
    // MARK: - Setup
    
    func setupUI() {
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .white
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
        
        tableView.tableHeaderView = header
    }
    
    func setupNavItems() {
        let searchBtn = UIButton(type: .custom)
        searchBtn.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
        searchBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
        searchBtn.setImage(UIImage(named: "ic_search"), for: .normal)
        searchBtn.addTarget(self, action: #selector(searchTapped), for: .touchUpInside)
        let searchBar = UIBarButtonItem(customView: searchBtn)
    
        navigationItem.rightBarButtonItems = [searchBar]
    }
}

extension UnarmedListVC: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = datas[indexPath.row]
        let ID : String = "UnarmedListCell"
            
        var cell = tableView.dequeueReusableCell(withIdentifier: ID)
        if cell == nil {
            cell = UnarmedListCell(style: .default, reuseIdentifier: ID)
        }
        
        (cell as? UnarmedListCell)?.reload(withModal: data)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 72
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let data = datas[indexPath.row]
        if data.sfczzj == "1" {
            let vc = UnarmedListVC(withDeptId: Int64(data.id ?? "-1"), deptName: data.label, date: date, label: label, val: val)
            navigationController?.pushViewController(vc, animated: true)
        } else {
            let vc = UnarmedDetailVC(withDeptId: Int64(data.id ?? "-1"), deptName: data.label, label: label, val: val)
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}
